package com.pozu.new_retail.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pozu.new_retail.dao.model.Goods;
import com.pozu.new_retail.dao.model.GoodsExt;
import com.pozu.new_retail.dao.mapper.GoodsExtMapper;
import com.pozu.new_retail.dao.mapper.GoodsMapper;
import com.pozu.new_retail.dao.mapper.ShoppingCartMapper;
import com.pozu.new_retail.dao.model.ShoppingCart;
import com.pozu.new_retail.dao.vo.ShoppingCartVo;
import com.pozu.new_retail.service.ShoppingCartService;
import com.pozu.new_retail.utils.CodeConstants;

@Service("shoppingCartService")
public class ShoppingCartServiceImpl implements ShoppingCartService {

	@Autowired
	private ShoppingCartMapper shoppingCartMapper;
	@Autowired
	private GoodsExtMapper goodsExtMapper;
	@Autowired
	private GoodsMapper goodsMapper;
	
	@Override
	public int saveShoppingCart(ShoppingCart shoppingCart) {
		
		long time = System.currentTimeMillis();
		
		shoppingCart.setCreatedTime(time);
		shoppingCart.setUpdatedTime(time);
		
		return shoppingCartMapper.insertShoppingCart(shoppingCart);
	}

	@Override
	public ShoppingCart queryByShoppingCartId(long shoppingCartId) {
		return shoppingCartMapper.queryByShoppingCartId(shoppingCartId);
	}

	@Override
	public int updateShoppingCart(ShoppingCart shoppingCart) {
		
		shoppingCart.setUpdatedTime(System.currentTimeMillis());
		
		return shoppingCartMapper.updateShoppingCart(shoppingCart);
	}

	@Override
	public HashMap<Integer, List<ShoppingCartVo>> queryByMap(HashMap<String, Object> map) {
		
		HashMap<Integer, List<ShoppingCartVo>> hashMap = new HashMap<Integer, List<ShoppingCartVo>>();
		
		List<ShoppingCartVo> list = shoppingCartMapper.queryByMap(map);
		
		if(null != list &&list.size()>0){
			for(ShoppingCartVo vo:list){
				if(null != vo){
					int goodsExtId = vo.getGoodsExtId();
					GoodsExt goodsExt = goodsExtMapper.queryByGoodsExtId(goodsExtId);
					if(null == goodsExt || goodsExt.getState() != 0 || goodsExt.getPrice() <= 0||goodsExt.getNumber() < vo.getNumber()){
						ShoppingCart cart = new ShoppingCart();
						cart.setShoppingCartId(vo.getShoppingCartId());
						cart.setState(-1);
						updateShoppingCart(cart);
						vo.setState(-1);
					}
					Goods goods = goodsMapper.queryInfoByGoodsId(vo.getGoodsId());
					if (null == goods || goods.getState() != 0) {
						ShoppingCart cart = new ShoppingCart();
						cart.setShoppingCartId(vo.getShoppingCartId());
						cart.setState(-1);
						updateShoppingCart(cart);
						vo.setState(-1);
					}
				}
				if(vo.getState() == 0){
					//正常单 0不可组团1可组团2砍价
					if (vo.getType() == 0) {
						if (hashMap.containsKey(0)) {
							List<ShoppingCartVo> vos = hashMap.get(0);
							vos.add(vo);
							hashMap.put(0, vos);
						}else {
							List<ShoppingCartVo> vos = new ArrayList<ShoppingCartVo>();
							vos.add(vo);
							hashMap.put(0, vos);
						}
					}else if (vo.getType() == 1) {
						if (hashMap.containsKey(1)) {
							List<ShoppingCartVo> vos = hashMap.get(1);
							vos.add(vo);
							hashMap.put(1, vos);
						}else {
							List<ShoppingCartVo> vos = new ArrayList<ShoppingCartVo>();
							vos.add(vo);
							hashMap.put(1, vos);
						}
					}else if (vo.getType() == 2) {
						if (hashMap.containsKey(2)) {
							List<ShoppingCartVo> vos = hashMap.get(2);
							vos.add(vo);
							hashMap.put(2, vos);
						}else {
							List<ShoppingCartVo> vos = new ArrayList<ShoppingCartVo>();
							vos.add(vo);
							hashMap.put(2, vos);
						}
					}
				}else if(vo.getState()==-1){
					//失效单
					if (hashMap.containsKey(-1)) {
						List<ShoppingCartVo> vos = hashMap.get(-1);
						vos.add(vo);
						hashMap.put(-1, vos);
					}else {
						List<ShoppingCartVo> vos = new ArrayList<ShoppingCartVo>();
						vos.add(vo);
						hashMap.put(-1, vos);
					}
				}
			}
		}
		
		return hashMap;
	}

	@Override
	public int checkShoppingCarts(List<ShoppingCart> carts,Integer codeDiscount) {
		
		for (ShoppingCart cart:carts) {
			if(null!=cart){
				
				int goodsExtId = cart.getGoodsExtId();
				
				GoodsExt ext = goodsExtMapper.queryByGoodsExtId(goodsExtId);
				
				if(null!=ext){

					int number = ext.getNumber();//库存数量
					
					if(cart.getNumber() > number){
						return CodeConstants.GOODSNOTENOUGH_MSG;
					}else {
						if (null != codeDiscount && cart.getType() != 1) {
							return CodeConstants.CODEUSEDEERROR_MSG;
						}
					}
				}
			}
		}
		
		return 0;
	}

	@Override
	public ShoppingCart queryByGoodsExtIdAndUserId(HashMap<String, Object> map) {
		return shoppingCartMapper.queryByGoodsExtIdAndUserId(map);
	}

	@Override
	public int queryMyCountByUserId(HashMap<String, Object> hashMap) {
		return shoppingCartMapper.queryMyCountByUserId(hashMap);
	}

}
